#include "SnakeProcess.h"
#include "RenderManager.h"
#include "Math\Matrix44.h"
#include "Font\FontManager.h"
#include "Core\Core.h"
#include "Utils\Base.h"
#include "InputManager.h"

//CVideogameProcess::CVideogameProcess()
//{
//}


CSnakeProcess::~CSnakeProcess()
{
	DeInit();
}

void CSnakeProcess::Init()
{
}

void CSnakeProcess::DeInit()
{
}

bool CSnakeProcess::isTouching(Vect2f x, Vect2i Point)
{


	float l_xMiddleX = (float) x.x + (CORE->GetFontManager()->SizeX("x")/2);
	float l_xMiddleY = (float) x.y + (CORE->GetFontManager()->SizeY("x")/2);
	float l_pMiddleX = (float) Point.x + (CORE->GetFontManager()->SizeX("X")/2);
	float l_pMiddleY = (float)  Point.y + (CORE->GetFontManager()->SizeY("X")/2);
	float l_MinDistanceX = (float) (CORE->GetFontManager()->SizeX("x") + CORE->GetFontManager()->SizeX("X"))/2-1;
	float l_MinDistanceY = (float) (CORE->GetFontManager()->SizeY("x") + CORE->GetFontManager()->SizeY("X"))/2-6;

	if(abs(l_xMiddleX-l_pMiddleX) < l_MinDistanceX && abs(l_xMiddleY-l_pMiddleY) < l_MinDistanceY)
	{
		return true;
	}


	return false;
}


void CSnakeProcess::Update(float dt)
{



	if(!m_bGameOver)
	{
		m_fAccTimeEnd += dt;

		if(m_fAccTimeEnd < 40)
		{

			m_fAccTimeSpeed += dt;
			if(m_fAccTimeSpeed > 10)
			{
				m_fAccTimeSpeed = 0;
				m_iSpeed += 100;
			}

			if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_UP))
			{
				m_eDirection = DIRECTION_UP;
			}
			else if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_DOWN))
			{
				m_eDirection = DIRECTION_DOWN;
			}
			else if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_LEFT))
			{
				m_eDirection = DIRECTION_LEFT;
			}
			else if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_RIGHT))
			{
				m_eDirection = DIRECTION_RIGHT;
			}


			switch(m_eDirection)
			{
			case(DIRECTION_UP):		m_Position.y -= dt*m_iSpeed;
									if(m_Position.y < 0)
										m_bGameOver = true;
									break;
			case(DIRECTION_DOWN):	m_Position.y += dt*m_iSpeed;
									if(m_Position.y > CORE->GetRenderManager()->GetScreenHeight())
										m_bGameOver = true;
									break;
			case(DIRECTION_RIGHT):	m_Position.x += dt*m_iSpeed;
									if(m_Position.x > CORE->GetRenderManager()->GetScreenWidth())
										m_bGameOver = true;
									break;
			case(DIRECTION_LEFT):	m_Position.x -= dt*m_iSpeed;
									if(m_Position.x < 0)
										m_bGameOver = true;
									break;

			}

			for(int i=0; i<NUM_POINTS; ++i)
			{
				if(m_bPointActive[i] == true)
				{
					if(isTouching(m_Position, m_vPoints[i]))
					{
						m_iScore += 10;
						m_bPointActive[i] = false;
					}
				}
			}

		}
		else
		{
			m_bGameOver = true;
		}
	}
	else
	{
		if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD, KEY_SPACE))
		{
			m_Position.x = (float) CORE->GetRenderManager()->GetScreenWidth()/2;
			m_Position.y = (float) CORE->GetRenderManager()->GetScreenHeight()/2;
			m_bGameOver = false;
			m_eDirection = DIRECTION_RIGHT;
			m_fAccTimeEnd = 0;
			m_iSpeed = 100;
			m_iScore = 0;
			m_bPointActive.clear();
			for(int i=0; i<NUM_POINTS; ++i)
			{
				m_bPointActive.push_back(true);
			}
			/*m_vPoints[0]	=	Vect2i(5, 3);
			m_vPoints[1]	=	Vect2i(50, 3);
			m_vPoints[2]	=	Vect2i(24, 5);
			m_vPoints[3]	=	Vect2i(214, 540);
			m_vPoints[5]	=	Vect2i(123, 123);
			m_vPoints[6]	=	Vect2i(333, 333);
			m_vPoints[7]	=	Vect2i(450, 56);
			m_vPoints[8]	=	Vect2i(50, 74);
			m_vPoints[9]	=	Vect2i(354, 12);
			m_vPoints[10]	=	Vect2i(300, 200);*/
			m_vPoints.clear();
			m_vPoints.push_back(Vect2i(10, 10));
			m_vPoints.push_back(Vect2i(40, 40));
			m_vPoints.push_back(Vect2i(70, 70));
			m_vPoints.push_back(Vect2i(150, 150));
			m_vPoints.push_back(Vect2i(200, 200));
			m_vPoints.push_back(Vect2i(250, 250));
			m_vPoints.push_back(Vect2i(300, 300));
			m_vPoints.push_back(Vect2i(400, 400));
			m_vPoints.push_back(Vect2i(500, 500));
			m_vPoints.push_back(Vect2i(600, 600));

		}
	}
}

void CSnakeProcess::RenderScene(CRenderManager* RM)
{

}

void CSnakeProcess::RenderScene2D(CRenderManager* RM, CFontManager* FM)
{
	if(m_bGameOver)
	{
		FM->DrawDefaultText((uint32)(CORE->GetRenderManager()->GetScreenWidth()/2 - FM->SizeX("Pulse ESPACIO para empezar")/2), (uint32)(CORE->GetRenderManager()->GetScreenHeight()/2), colWHITE, "Pulse ESPACIO para empezar");
	}
	else
	{
		FM->DrawDefaultText((uint32)m_Position.x, (uint32)m_Position.y, colWHITE, "x");
		int pos = FM->SizeY("T")*2;
		FM->DrawDefaultText(0, (uint32)(CORE->GetRenderManager()->GetScreenHeight() - pos), colWHITE, "Time: %f", m_fAccTimeEnd);
		FM->DrawDefaultText(0, (uint32)(CORE->GetRenderManager()->GetScreenHeight() - (pos/2)), colWHITE, "Score: %i", m_iScore);
		
		for(int i=0; i<NUM_POINTS; ++i)
		{
			if(m_bPointActive[i] == true)
			{
				FM->DrawDefaultText((uint32)m_vPoints[i].x, (uint32)m_vPoints[i].y, colRED, "X");
			}
		}
	
	}
}